<?php

namespace common\app;


use yii;

class Authcode
{
    private $key = 'auth-code';

    function create($len = 4, $width = 100, $height = 30)
    {
        /**
         * 字母+数字的验证码生成
         */

//1.创建黑色画布
        $image = imagecreatetruecolor($width, $height);

//2.为画布定义(背景)颜色
        $bgcolor = imagecolorallocate($image, 255, 255, 255);

//3.填充颜色
        imagefill($image, 0, 0, $bgcolor);

// 4.设置验证码内容

//4.1 定义验证码的内容
        $content = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

//4.1 创建一个变量存储产生的验证码数据，便于用户提交核对
        $captcha = "";
        for ($i = 0; $i < $len; $i++) {
            // 字体大小
            $fontsize = 14;
            // 字体颜色
            $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
            // 设置字体内容
            $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
            $captcha .= $fontcontent;
            // 显示的坐标
            $x = ($i * $width / $len) + mt_rand(2, 5);
            $y = mt_rand(5, 20);
            // 填充内容到画布中
            imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
        }
        $key = $this->key;
        Yii::$app->session->set($key,$captcha);

//4.3 设置背景干扰元素
        for ($$i = 0; $i < 200; $i++) {
            $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imagesetpixel($image, mt_rand(1, $width), mt_rand(1, $height), $pointcolor);
        }

//4.4 设置干扰线
        for ($i = 0; $i < 3; $i++) {
            $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
            imageline($image, mt_rand(1, $width), mt_rand(1, $height), mt_rand(1, $width), mt_rand(1, $height), $linecolor);
        }

//5.向浏览器输出图片头信息
        header('content-type:image/png');

//6.输出图片到浏览器
        imagepng($image);

//7.销毁图片
        imagedestroy($image);
    }

    function valid($code)
    {
        $key = $this->key;
        $_code = isset($_SESSION["{$key}"]) ? $_SESSION["{$key}"] : '';
        if (empty($_code))
            return false;
        if (strtolower($_code) == strtolower($code))
            return true;
        return false;
    }
}

